<?php 
           use_stylesheet('list.css');
           $labels=sfConfig::get("app_var_label");
           $floats=array("kW","W/mq","kWh","kVarh","°C","m/s","V","A");
           $chartData=array();
           
           foreach($data as $plant=>$plantData){
              foreach($plantData as $dev=>$devData){
                if (count($devData,COUNT_NORMAL)>0) { 
                    foreach($devData as $row){ 
                        foreach($row as $varName=>$val){ 
                             if (($varName == 'id')) continue;
                             if (($varName=="sampletime")){
                                 $tmp=new DateTime($val);
                                 //all the sample time of the given series are
                                 //appended in the end of the array
                                 $time[]=$tmp->format("d/m/y H:i");
                             }else{
                                  //replace vars with the configured labels
                                    if (key_exists($varName, $labels))
                                        $label=$labels[$varName];
                                    else
                                        $label=$varName;
                                    
                                    if (strpos($label, "_")!== FALSE){
                                        $labelName=substr($label,0, strpos($label, "_"));
                                        $um=substr($label,strpos($label, "_")+1);
                                    }else{
                                        $labelName=$label;
                                        $um=NULL;
                                    }
                                    
                                    $chartData[$dev.": ".$labelName." [".$um."]"][$tmp->format("d/m/y H:i")]= floatval($val);     
                             }
                                
                                     
                        }           
                    }
                }else{ ?>
                    <div class="redlight"> Query on <?php echo $dev?> returned no data </div>
        <?php   }
            }

            if (count($chartData)>0){
                
                //time processing
                //sort the time array
                sort($time);
                //remove duplicates sample time
                $time=array_values(array_unique($time));
                
                $rainbow = array('red','purple','orange','blue','yellow','green',);
                $i=0;
               
                $minScale=NULL;
                $maxScale=NULL;
                $vAxes=array();
                $umsPresent=array();                
                foreach($chartData as $title=>$series){
                    
                    //adjust data series on the global time base
                    foreach($time as $timeKey=>$timeVal){
                        $newChartData[$title][$timeKey]=(isset($series[$timeVal]))?$series[$timeVal]:'null';
                    }
                    
                    //configure chart options
                    if (strpos($title,"[")!== FALSE)
                        $um=substr($title,strpos($title, "[")+1);
                    else
                        $um=NULL;
                    
                    if (key_exists($um, $umsPresent)!==FALSE)
                        $vAxesIndex=$umsPresent[$um];
                    else{
                        if ($i<2){
                            $vAxesIndex=$i;
                            $vAxes[$vAxesIndex]=array("title"=>$title, "maxValue"=>max($series), "minValue"=>min($series), "textStyle"=>array("color"=>$rainbow[($i % count($rainbow))] ), "titleTextStyle"=>array("color"=>$rainbow[($i % count($rainbow))] ) );
                        }else
                            $vAxesIndex=1;
                        
                        $umsPresent[$um]=$vAxesIndex;
                    }       
                    $seriesFormat[$i]=array("color"=>$rainbow[($i % count($rainbow))], "visibleInLegend"=>True, "targetAxisIndex"=>$vAxesIndex);
//                    if (!isset($minScale) or ($minScale>min($series)))
//                            $minScale=min($series);
//                    if (!isset($maxScale) or ($maxScale>max($series)))
//                            $maxScale=  max($series);
                    
                    $i++;
                }

                use_helper('sdInteractiveChart');
                $chart = InteractiveChart::newLineChart();              
                $chart->setWidthAndHeight('650', '400');
                $chart->setOption("series", $seriesFormat);
                $chart->setOption("vAxes", $vAxes);
                $chart->setOption("interpolateNulls", true);
                $chart->setBaselineColor('#680000');
                $chart->setHorizontalAxisTitle("Time");
                $chart->inlineGraph($newChartData, $time,'chart_div');
                $chart->render(); 
                ?>
                <div class="chart" id="chart_div" ></div>
  <?php       } 
            }?> 


            
       